package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.annotation.AutoFill;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.enumeration.OperationType;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author 山顶洞人郑某
 * @className DishMapper.java
 * @date 2024 01 20
 */
@Mapper
public interface DishMapper {


    /**
     * 添加菜品
     * @param dish
     */
    @AutoFill(value = OperationType.INSERT)
    void save(Dish dish);

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    Page<DishVO> page(DishPageQueryDTO dishPageQueryDTO);

    /**
     * 根据id查询菜品状态
     * @param id
     * @return
     */
    @Select("select status from sky_take_out.dish where id = #{id}")
    Integer getStatus(Long id);

    /**
     * 根据id删除菜品
     * @param id
     */
    @Delete("delete from sky_take_out.dish where id = #{id}")
     void deleteById(Long id);

    /**
     * 根据id查询菜品信息
     * @param id
     * @return
     */
    Dish getById(Long id);

    /**
     * 根据id修改菜品信息
     * @param dish
     */
    @AutoFill(OperationType.UPDATE) //自动填充公共字段
    void updateById(Dish dish);

    /**
     * 根据菜品分类id查询菜品
     * @param categoryId
     * @return
     */
    @Select("select * from sky_take_out.dish where category_id = #{categoryId} and status =1 order by create_time desc")
    List<Dish> list(Long categoryId);

    @Select("select s.name,s.copies,d.image,d.description from sky_take_out.setmeal_dish as s left join sky_take_out.dish as d" +
            " on s.dish_id = d.id where s.setmeal_id = #{id}" )
    List<DishItemVO> getListByCategoryId(Long id);


    @Select("select d.id, d.name, category_id, price, image, description, d.status, d.update_time,c.name as categoryName" +
            " from sky_take_out.dish as d left join sky_take_out.category as c on d.category_id = c.id " +
            " where d.category_id = #{categoryId} and d.status = #{status} order by d.update_time desc ")
    List<DishVO> userList(Dish dish);

    /**
     * 根据状态查询菜品数量
     * @param status
     * @return
     */
    @Select("select count(id) from sky_take_out.dish where status = #{status}")
    Integer getSoldAnddiscontinued(Integer status);
}
